package com.ylw.project.handler.impl;

import com.ylw.project.Tools;
import com.ylw.project.constant.UIResources;
import com.ylw.project.handler.Handler;
import com.ylw.project.handler.chain.HandlerChain;
import com.ylw.util.Config;

import java.io.File;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 用来处理html css等前端页面
 */
public class UIResourcesHandler implements Handler {


    @Override
    public Object handler(HandlerChain chain, String filePath) {

        if (check(filePath)) {
            String outputPath =getOutputpath(filePath);
            Tools.copyFile(filePath, outputPath);
        }
        else {
            chain.doHandler(chain.nextHandler());
        }
        return null;
    }

    private boolean check(String filePath) {

        return UIResources.endWith(filePath);
    }

    private String getOutputpath(String path) {

        List<String> webSourceRoot = Config.getPropertValue("project.source.web.root");
        String rootPath = "";
        String relaPath = "";
        for(String ss :webSourceRoot) {
            rootPath = Tools.getOutputProjectRootRealPath(path, ss);
            if("".equals(rootPath)) {
                continue;
            }
            Matcher m = Pattern.compile("\\\\" + ss + "\\\\(.+)").matcher(path);

            if(Tools.isMacOs()) {
                m = Pattern.compile("/" + ss + "/(.+)").matcher(path);
            }
            if (m.find()) {
                relaPath = m.group(1);
                break;
            }
        }
        return rootPath+File.separator+relaPath;
    }

    public static void main(String[] args) {
        String path = "D:\\ideaproject1\\wap3.0\\webcontext\\market\\broadbandContinue\\rhOrderSub_new.whtml";

        String sss = new UIResourcesHandler().getOutputpath(path);
        System.out.println(sss);


        String ss  = "web";
        String aa  = "webcontext";
        Matcher m = Pattern.compile("\\\\" + ss + "\\\\(.+)").matcher(path);
        while(m.find()){
            for(int i  =0;i<=m.groupCount();i++){
                System.out.println(m.group(i));
            }
        }
    }
}
